import router, { constantRoutes } from '@/router'
import { getRouters } from '@/api/menu'
import Layout from '@/layout/index'
const permission = {
  state: {
    routes: [],
    addRoutes: [],
    defaultRoutes: [],
    topbarRouters: [],
    sidebarRouters: [],
    permissionList: [],
  },
  mutations: {
    SET_NAME: (state, name) => {
      localStorage.setItem('name', name)
      state.name = name
    },
    SET_ROUTES: (state, routes) => {
      state.addRoutes = routes
      state.routes = constantRoutes.concat(routes)
    },
    SET_DEFAULT_ROUTES: (state, routes) => {
      state.defaultRoutes = constantRoutes.concat(routes)
    },
    SET_TOPBAR_ROUTES: (state, routes) => {
      state.topbarRouters = routes
    },
    SET_SIDEBAR_ROUTERS: (state, routes) => {
      state.sidebarRouters = routes
    },
  },
  actions: {
    // 生成路由
    GenerateRoutes({ commit }) {
      return new Promise((resolve, reject) => {
        // 向后端请求路由数据
        getRouters().then(res => {
          if (res.data) {
            let names = {}
            names.userName = res.data.userInfo.account
            names.phoneNumber = res.data.userInfo.phoneNumber
            names.name = res.data.userInfo.username
            names.userAvatarUrl = res.data.userInfo.userAvatarUrl
            commit('SET_NAME', names)
            const asyncRoutes = listRouter(res.data.userInfo.menuList)
            router.addRoutes(asyncRoutes);
            commit('SET_ROUTES', asyncRoutes)
            commit('SET_SIDEBAR_ROUTERS', constantRoutes.concat(asyncRoutes))
            commit('SET_DEFAULT_ROUTES', asyncRoutes)
            commit('SET_TOPBAR_ROUTES', asyncRoutes)
            resolve(asyncRoutes)
          } else {
            reject(res)
          }
        })
      })
    }
  }
}
function listRouter(RouterData) {
  let newRouter = []
  let newarr = []
  let full =[]
  RouterData.map(item => {
    if (item.parentId == '0') {
      item.children = []
      let Fdata = {
        path: item.path,
        component: Layout,
        children: item.children,
        redirect: item.path,
        meta: {
          title: item.menuName,
          icon: item.icon
        }
      }
      full.push(item)
      newRouter.push(Fdata)
    } else {
      newarr.push(item)
    }
  })
  full.map(item => {
    newarr.map(v => {
      let indexAV = v.path.indexOf('/')
      let indexBV = v.path.indexOf('/', indexAV + 1)
      if (item.menuId == v.parentId) {
        let data = {
          name: indexBV == '-1' ? v.path.slice(indexAV + 1) : v.path.slice(indexBV + 1),
          path:v.query?v.path+ v.query:v.path,
          component: loadView(v.component),
          meta: {
            title: v.menuName,
            icon: v.icon
          },
          hidden: v.visible == 1 ? true : false
        }
        item.children.push(data)
      }
    })
  })
  return newRouter
}
//生成动态路由component路径
export const loadView = (view) => {
  if (process.env.NODE_ENV === 'development') {
    return (resolve) => require([`@/views${view}`], resolve)
  } else {
    // 使用 import 实现生产环境的路由懒加载
    return () => import(`@/views${view}`)
  }
}

export default permission
